//
//  StandardLog.m
//  AFNetworking
//
//  Created by Huadong Zheng on 2023/4/11.
//

#import "StandardLog.h"





static void sz_convertToJsonString(id obj, NSUInteger level, NSMutableString *desc, NSString *tab, NSString *key) {
    NSError *error = nil;
    NSObject *result = [NSJSONSerialization JSONObjectWithData:obj
                                                        options:NSJSONReadingMutableContainers
                                                          error:&error];
    if (key) {
        key = [key stringByAppendingString:@" : "];
    } else {
        key = @"";
    }
    if (error == nil && result != nil) {
        if ([result isKindOfClass:[NSDictionary class]]
            || [result isKindOfClass:[NSArray class]]) {
            NSString *str = [((NSDictionary *)result) descriptionWithLocale:nil indent:level + 1];
            [desc appendFormat:@"%@\t%@,\n", tab, str];
        } else if ([result isKindOfClass:[NSString class]]) {
            [desc appendFormat:@"%@\t%@\"%@\",\n", tab, key, result];
        }
    } else {
        @try {
            NSString *str = [[NSString alloc] initWithData:obj encoding:NSUTF8StringEncoding];
            if (str != nil) {
                [desc appendFormat:@"%@\t%@\"%@\",\n", tab, key, str];
            } else {
                [desc appendFormat:@"%@\t%@%@,\n", tab, key, obj];
            }
        }
        @catch (NSException *exception) {
            [desc appendFormat:@"%@\t%@%@,\n", tab, key, obj];
        }
    }
}

@implementation NSDictionary (Log)

- (NSString *)descriptionWithLocale:(id)locale{
    
    return [self descriptionWithLocale:locale indent:0];

}
- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level{
    NSMutableString *desc = [NSMutableString string];
    NSMutableString *tabString = [[NSMutableString alloc] initWithCapacity:level];
    for (NSUInteger i = 0; i < level; ++i) {
        [tabString appendString:@"\t"];
    }
    NSString *tab = @"";
    if (level > 0) {
        tab = tabString;
    }
    [desc appendString:@"{\n"];
    // 对字典排序
    NSArray *allkeys = [self.allKeys sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        return [obj1 compare:obj2];
    }];
    
    for (id k in allkeys) {
        id obj = [self objectForKey:k];
        NSString *key = k;
        if ([key isKindOfClass:[NSString class]]) {
            key = [NSString stringWithFormat:@"\"%@\"", key];
        }
        if ([obj isKindOfClass:[NSString class]]) {
            [desc appendFormat:@"%@\t%@: \"%@\",\n", tab, key, obj];
        } else if ([NSStringFromClass([obj class]) isEqualToString:@"__NSCFBoolean"]) {
            [desc appendFormat:@"%@\t%@: %s,\n", tab, key, [(NSNumber *)obj boolValue] ?"true": "false"];
        } else if ([obj isKindOfClass:[NSArray class]]
                   || [obj isKindOfClass:[NSDictionary class]]) {
            [desc appendFormat:@"%@\t%@: %@,\n", tab, key, [obj descriptionWithLocale:locale indent:level + 1]];
        } else if ([obj isKindOfClass:[NSData class]]) {
            // 如果是NSData类型，尝试去解析结果，以打印出可阅读的数据
            sz_convertToJsonString(obj, level, desc, tab, key);
        } else if ([obj isKindOfClass:[NSNull class]])  {
            [desc appendFormat:@"%@\t%@: null,\n", tab, key];
        } else {
            [desc appendFormat:@"%@\t%@: %@,\n", tab, key, obj];
        }
    }
    // 查出最后一个,的范围
    NSRange range = [desc rangeOfString:@"," options:NSBackwardsSearch];
    if (range.length) {
        // 删掉最后一个,
        [desc deleteCharactersInRange:range];
    }
    [desc appendFormat:@"%@}", tab];
    return desc;
}
@end

@implementation NSArray (Log)

- (NSString *)descriptionWithLocale:(id)locale{

    
    return [self descriptionWithLocale:locale indent:0];
}
- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level{
    NSMutableString *desc = [NSMutableString string];
    NSMutableString *tabString = [[NSMutableString alloc] initWithCapacity:level];
    for (NSUInteger i = 0; i < level; ++i) {
        [tabString appendString:@"\t"];
    }
    NSString *tab = @"";
    if (level > 0) {
        tab = tabString;
    }
    [desc appendString:@"[\n"];
    for (id obj in self) {
        if ([obj isKindOfClass:[NSDictionary class]]
            || [obj isKindOfClass:[NSArray class]]) {
            NSString *str = [obj descriptionWithLocale:locale indent:level + 1];
            [desc appendFormat:@"%@\t%@,\n", tab, str];
        } else if ([obj isKindOfClass:[NSString class]]) {
            [desc appendFormat:@"%@\t\"%@\",\n", tab, obj];
        } else if ([NSStringFromClass([obj class]) isEqualToString:@"__NSCFBoolean"]) {
            [desc appendFormat:@"%@\t%s,\n", tab, [(NSNumber *)obj boolValue] ?"true": "false"];
        } else if ([obj isKindOfClass:[NSData class]]) {
            // 如果是NSData类型，尝试去解析结果，以打印出可阅读的数据
            sz_convertToJsonString(obj, level, desc, tab, nil);
        } else if ([obj isKindOfClass:[NSNull class]])  {
            [desc appendFormat:@"%@\tnull,\n", tab];
        }  else {
            [desc appendFormat:@"%@\t%@,\n", tab, obj];
        }
    }
    // 查出最后一个,的范围
    NSRange range = [desc rangeOfString:@"," options:NSBackwardsSearch];
    if (range.length) {
        // 删掉最后一个,
        [desc deleteCharactersInRange:range];
    }
    [desc appendFormat:@"%@]", tab];
    return desc;
}


@end
